package myJs.api

import japgolly.scalajs.react.extra.Ajax
import myJs.myPkg.MyFetch
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.Pojo.PreprocessSoftData
import shared.pojo.Pojo.{ValidData, data}
import upickle.default._
import myJs.Implicits._
import shared.pojo.WorkflowMission.MissionNameCheck
import shared.pojo.mission._

/** Created by yz on 18/10/2022
  */
object Mission {

  val baseUrl = s"${Api.apiUrl}/user/mission"

  def getConfigData(missionId: String) = {
    val url = s"${baseUrl}/getConfigData?missionId=${missionId}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        xhr.responseText
      }
  }

  def nameCheck(request: MissionNameCheck.Request) = {
    val url = s"${baseUrl}/nameCheck"
    MyFetch
      .postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json"))
      .map { str =>
        read[ValidData](str)
      }
  }

  def canPaired(request: CanPaired.Request) = {
    val url = s"${baseUrl}/canPaired"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[Boolean](xhr.responseText)
      }
  }

}
